<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class NetdiskController extends CUserController {
    
    
    public function actionIndex() {
        $browser = new Browser();
        if($browser->getBrowser() == Browser::BROWSER_IE) {
            $version = $browser->getVersion();
            if($version == "6.0" || $version == "7.0") {
                $this->render('changeBrowser');
                Yii::app()->end();
            }
        }
        
        $user = Yii::app()->session["user"];
        $nick = $user["name"];
        if(isset($user["nick"]) && ! empty($user["nick"])) {
            $nick = $user["nick"];
        }
        
        $user = Yii::app()->session["user"];
        $user_id = $user["id"];
        
        $totalSpace = CUtils::totalSize($user_id);
        $used = UserFile::model()->getUsedSpace($user_id);
        $usedSpace = 0;
        if(count($used) > 0) {
            $usedSpace = $used[0]["usedSpace"];
        }
        
        $page = isset($_SESSION['setting/index']) ? 'account' : "u";
        unset($_SESSION['setting/index']);
        
        $userBase = new UserBase();
        $userBase->loadFull($user_id);
        $isAdmin = $user["isAdmin"];
        $this->render('index', array('nick' => $nick, 'isAdmin' => $isAdmin, 'totalSpace' => doubleval($totalSpace), 'usedSpace' => doubleval($usedSpace), 'totalSpaceText' => CUtils::getFormatSize($userBase->space),                 'usedSpaceText' => CUtils::getFormatSize($userBase->usedSpace),                 'per' => round($userBase->usedSpace * 100 / $userBase->space, 2),                 'page' => $page));     
    }
    
    public function actionFrameTopNav() {
        $user = Yii::app()->session["user"];
        $nick = $user["nick"];
        if(! isset($nick)) {
            $nick = $user["name"];
        }
        $this->render('frameTopNav', array("nick" => $nick));
    }
    
    public function actionHomepage() {
        $user = Yii::app()->session["user"];
        $userId = $user["id"];
        $userBase = new UserBase();
        $userBase->loadFull($userId);
        $this->render('homepage', array('userBase' => $userBase, 'space' => CUtils::getFormatSize($userBase->space),                 'usedSpace' => CUtils::getFormatSize($userBase->usedSpace),                 'over' => CUtils::getFormatSize($userBase->space - $userBase->usedSpace),                 'per' => round($userBase->usedSpace * 100 / $userBase->space, 2)));     
    }
    
    public function actionFolder() {
        $user = Yii::app()->session["user"];
        $userId = $user["id"];
        $this->renderText(UserFile::model()->getAllFolderJson($userId));
    }
    
    public function actionTrash() {
        $user = Yii::app()->session["user"];
        $userId = $user["id"];
        $count = UserFile::model()->getTrashCount($userId);
        $this->renderText(json_encode(array("count" => $count)));
    }
    
    public function actionUpload() {
        $user = Yii::app()->session["user"];
        $userId = $user["id"];
        
        $cid = Yii::app()->request->getParam('cid', '0');
        
        if(count($_FILES) == 0) {
            echo CJSON::encode(CUtils::retMessage(false, 0, "传入参数错误"));
            Yii::app()->end();
        }
        
        $keys = array_keys($_FILES);
        if(count($keys) != 1) {
            echo CJSON::encode(CUtils::retMessage(false, 0, "传入参数错误"));
            Yii::app()->end();
        }
        $key = $keys[0];
                                if(isset($_FILES[$key]) === false) {
            echo CJSON::encode(CUtils::retMessage(false, 0, "传入参数错误"));
            Yii::app()->end();
        }
                                if($_FILES[$key]["error"] != 0) {
            echo CJSON::encode(CUtils::retMessage(false, 0, "传入参数错误"));
            ;
            Yii::app()->end();
        }
        
        $file_name = $_FILES[$key]["name"];
        $type = $_FILES[$key]["type"];
        $size = $_FILES[$key]["size"];
        $tmp_name = $_FILES[$key]["tmp_name"];
        
        if(CUtils::checkNameInvalid($file_name) != 0) {
            echo CJSON::encode(CUtils::retMessage(false, 0, "命名不能包含下列字符串： ^|?*\\<\":>"));
            ;
            Yii::app()->end();
        }
        
        $create = new CreateFile();
        $create->_userId = $userId;
        $create->cname = $file_name;
        $create->toId = $cid;
        $create->tmpName = $tmp_name;
        $create->size = $size;
        $create->invoke(false);
                if($create->result["state"] == false) {
            $create->handleEnd();
        }
        
        $tt = Yii::app()->params["app"]["defaultSpace"];
        $total = Yii::app()->params["app"]["defaultSpace"] * 1024 * 1024;
        $used = UserFile::model()->getUsedSpace($userId);
        $usedSpace = $used[0]["usedSpace"];
        $upload_size_remain = $total - $usedSpace;
        $pathInfo = CUtils::pathinfo_utf($create->path);
        
        $data = Array();
        $data["is_user"] = 1;
        $data["user_id"] = $userId;
        $data["group_id"] = 0;
        $data["aid"] = 0;
        $data["user_ip"] = "0.0.0.0";
        $data["upload_type"] = 1;
        $data["temp_file_path"] = $create->cname;
        $data["temp_file_name"] = $create->cname;
        $data["temp_file_sha1"] = $create->hash;
        $data["is_share"] = 0;
        $data["file_ext"] = $pathInfo["extension"];
        $data["file_name_sort"] = strlen($create->cname);
        $data["file_size"] = $size;
        $data["file_description"] = "";
        $data["pick_time"] = "";
        $data["is_collect"] = 0;
        $data["file_status"] = 1;
        $data["file_ptime"] = time();
        $data["file_id"] = $create->fileId;
        $data["ico"] = $pathInfo["extension"];
        $data["area_id"] = $cid;
        $data["category_id"] = $cid;
        $data["upload_size_remain"] = $upload_size_remain;
        
        $ret = Array();
        $ret["state"] = true;
        $ret["data"] = $data;
        
        echo CJSON::encode($ret);
        Yii::app()->end();
    }
    
    
    public function actionGetPhotos() {
        $ne_lat = Yii::app()->request->getParam('ne_lat');
        $ne_lng = Yii::app()->request->getParam('ne_lng');
        $sw_lat = Yii::app()->request->getParam('sw_lat');
        $sw_lng = Yii::app()->request->getParam('sw_lng');
        $fileHandle = new AlbumList();
        $photos=$fileHandle->getPhotos($ne_lat, $ne_lng, $sw_lat, $sw_lng);
        echo json_encode($photos);
    }

}